<?php
   class BusinessAction extends CommonAction{
   	public function _filter(&$map)
   	{
   		$map['removed']=1;//1为未删除，2为回收站
   		$companyname=$_REQUEST['companyname'];
   		if(!empty($companyname))
   		{
   			$map['companyname']=array('like','%'.$companyname.'%');
   		}
   		
//    		if('1'!==$_SESSION [C ( 'USER_AUTH_KEY' )]){
//    			$map['userid']=$_SESSION [C ( 'USER_AUTH_KEY' )];
//    		}
   		$this->assign('companyname',$companyname);
   	}
	   	public function _before_index() {
	   		//默认管理员的id为1
	   		$userid=$_SESSION[C('USER_AUTH_KEY')];
	   		// $role=D('Role');
	   		/* if($userid=='1')
	   		{
	   			$this->assign('flag','isAdmin');
	   		} */
	   		//判断是否有业务的写权限
	   		$bs=$this->isBusiness($userid);
	   		if($bs||$userid==1)
	   		{
	   			$this->assign('flag','flag');
	   		}
	   		$model = M ( "Business" );
	   		//$list = $model->select();
	   		//$this->assign ( 'list', $list );
	   		$this->assign('userid',$userid);
	   	}
	   	public function _before_add() {
	   		$userid=$_SESSION[C('USER_AUTH_KEY')];
	   		//$role=D('Role');
	   		//管理员的id默认为1
	   		//获得是否具有写权限的
	   		$bs=$this->isBusiness($userid);
	   		//有写权限同时又不是管理员的
	   		if($bs&&$userid!=1)
	   		{
	   			$this->assign('flag','isbusiness');
	   		}
	   		$model = M ( "Business" );
	   		$list = $model->select ();
	   		$this->assign ( 'list', $list );
	   	}
	   	public function _before_edit() {
	   		$id=$_REQUEST['id'];
	   		$model = M ( "Business" );
	   		$list = $model->select ();
	   		$this->assign ( 'list', $list );
	   		$this->getgoodslist($id);
	   	}
	   	public function read()
	   	{
	   		$model = M ( "Business" );
	   		$id=$_REQUEST['id'];
	   		$viewid=$_SESSION[C('USER_AUTH_KEY')];
	   		$viewname=$_SESSION['username'];
	   		$list=$model->where('id='.$id)->find();
	   		$uid=$list['userid'];
	   		$u=M('User');
	   		$uname=$u->where('id='.$uid)->getField('username');
	   		$vid=$list['viewids'];
	   		$vname=$list['viewnames'];
	   		$data=array();
	   		if($viewid!=$uid){
	   		if(!empty($vid))
	   		{
	   			$data['viewids']=$vid.','.$viewid;
	   		}
	   		else 
	   		{
	   			$data['viewids']=$viewid;
	   		}
	   		if(!empty($vname))
	   		{
	   			$data['viewnames']=$vname.','.$viewname;
	   		}
	   		else
	   		{
	   			$data['viewnames']=$viewname;
	   		}
	   		}
	   		//$data['viewtime']=date('Y-m-d');
	   		
	   		$model->data($data)->where('id='.$id)->save();
	   		
	   		
	   		$this->getgoodslist($id);
	   		$this->assign('uname',$uname);
	   		$result=$model->where('id='.$id)->find();
	   		$this->assign("vo",$result);
	   		$this->display();	
	   	}
	   	//拟定合同
	   	public function draft()
	   	{
	   		//业务的id
	   		$id=$_REQUEST['id'];
	   		//跳转到合同添加的页面
	   		$this->redirect('Contract/add','id='.$id);
	   		//$this->ajaxReturn($id,'跳转',200);
	   		
	   	}
	   	
	   	//判断是不是具有写权限
	   	public function isBusiness($id)
	   	{
	   		$model=D('Business');
	   		//$table=$model->tablePrefix.'access';
	   		$rs=$model->query('select * from mlt_access a ,mlt_role_user b where node_id=122 and pid=85 and level=3 and a.role_id=b.role_id and b.user_id='.$id);
	   		if($rs==true)
	   		{
	   			return true;
	   		}
	   		else
	   		{
	   			return false;
	   		}
	   	}
	   	
	   	
	   	public function goodsadd()
	   	{
	   		$insert = $_REQUEST['insert']; 
	   		if($insert=='true'){
	   			$this->goodsinsert();
	   		}else{
	   			$id=$_REQUEST['id'];
	   			$this->assign('id',$id);
	   			$this->getGoods();
	   			$this->display();
	   		}
	   	}
	   	
	   	public function goodsinsert()
	   	{
	   		$bussinessid=$_REQUEST['bussiness'];
	   		//$goodname=$_REQUEST['goodname'];
	   		$model=M('BusinessGood');
	   	if (false === $model->create ()) {
			$this->error ( $model->getError () );
		    }
		    
		    //保存当前数据对象
		    $list=$model->add ();
		    if ($list!==false) { //保存成功
		    	//$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
		    	$this->insertLog('添加','操作业务添加商品，编号为'.$list.'的数据成功');
		    	//$this->success('添加成功','__URL__/index');
		    	$this->redirect('Business/index');
		    	//$this->ajaxReturn (1,'新增成功!',200);
		    } else {
		    	//失败提示
		    	$this->insertLog('添加','操作操作业务添加商品,编号为'.$list.'的数据失败');
		    	//$this->ajaxReturn (1,'新增失败!',300);
		    	$this->error ( $model->getError () );
		    }
		    
	   	}
	   	
	   	//获得商品信息
	   	public function getGoods()
	   	{
	   		$model=M('Goods');
	   		$list=$model->select();
	   		$this->assign("goods",$list);
	   	}
	   	
	   	//根据业务id获得业务所对应的商品列表
	   	public function getgoodslist($id)
	   	{
	   		$bgoods=M('BusinessGood');
	   		$bglist=$bgoods->where('bussinessid='.$id)->select();
	   		$this->assign('goods',$bglist);
	   	}
	   	
	   	public function goodsedit()
	   	{
	   		$id=$_REQUEST['id'];
	   		$bgood=M('BusinessGood');
	   		$list=$bgood->where('id='.$id)->find();
	   		$this->assign('good',$list);
	   		$this->getGoods();
	   		$this->display();
	   	}
	   	
	   	public function goodsupdate()
	   	{
	   		$id=$_REQUEST['id'];
	   		$bid=$_REQUEST['bid'];
	   		
	   		$model=M('BusinessGood');
	   	
	   		if (false === $model->create ()) {
	   			$this->error ( $model->getError () );
	   		}
	   		
	   		//保存当前数据对象
	   		$list=$model->where('id='.$id)->save ();
	   		if ($list!==false) { //保存成功
	   			//$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
	   			$this->insertLog('添加','操作业务修改商品，编号为'.$list.'的数据成功');
	   			//$this->success('添加成功','__URL__/index');
	   			$this->redirect('Business/edit','id='.$bid);
	   			//$this->ajaxReturn (1,'新增成功!',200);
	   		} else {
	   			//失败提示
	   			$this->insertLog('添加','操作操作业务修改商品,编号为'.$list.'的数据失败');
	   			//$this->ajaxReturn (1,'新增失败!',300);
	   			$this->error ( $model->getError () );
	   		}
	   		
	   		
	   	}
	   	
	   	public function goodsdel()
	   	{
	   	  $id=$_REQUEST['id'];	
	   	  $bid=$_REQUEST['bid'];
	   	  $bgood=M('BusinessGood');
	   	 $result= $bgood->where('id='.$id)->delete();
	   	 if($result)
	   	 {
	   	 	$this->insertLog('添加','操作业务修改商品的数据成功');
	   	 	//$this->success('添加成功','__URL__/index');
	   	 	$this->redirect('Business/edit','id='.$bid);
	   	 	
	   	 }
	   	 else
	   	 {
	   	 	$this->insertLog('添加','操作操作业务删除商品的数据失败');
	   	 	//$this->ajaxReturn (1,'新增失败!',300);
	   	 	$this->error ( $bgood->getError () );
	   	 }
	   	}
	   	
	   	/*   //判断是否是管理员
	   	 public function isAdmin()
	   	 {
	   	$table = $this->tablePrefix.'access';
	   	$rs = $this->db->query('select * from '.$table.' as a, '.$this->tablePrefix.'role_user as b  where user_id=1 and a.role_id=b.role_id ');
	   	if($rs==false)
	   	{
	   	return false;
	   	}
	   	else
	   	{
	   	return true;
	   	}
	   	} */
   }
?>